using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DailyTestBLL.XMLParse
{
    /// <summary>
    /// represent a tag
    /// </summary>
    public class XMLTag
    {
        #region fields

        private string m_TagContext;
        private string m_TagName;
        private Hashtable m_htTagAttribute;
        private int m_iBeginPos;
        private int m_iInternalPos1;
        private int m_iInternalPos2;
        private int m_iEndPos;
        private IList<XMLTag> m_lstChindren;

        #endregion

        #region properties

        /// <summary>
        /// hold the tag's context
        /// </summary>
        public string TagContext
        {
            get { return m_TagContext; }
            set { m_TagContext = value; }
        }

        /// <summary>
        /// hold the tag's name
        /// </summary>
        public string TagName
        {
            get { return m_TagName; }
            set { m_TagName = value; }
        }

        /// <summary>
        /// hold the tag's Attribute
        /// </summary>
        public Hashtable TagAttribute
        {
            get { return m_htTagAttribute; }
            set { m_htTagAttribute = value; }
        }

        /// <summary>
        /// hold the tag's begin pos of one document
        /// beginPos<head>internalPos1 internalPos2</head>endPos
        /// </summary>
        public int BeginPos
        {
            get { return m_iBeginPos; }
            set { m_iBeginPos = value; }
        }

        /// <summary>
        /// hold the tag's end pos of one document
        /// beginPos<head>internalPos1 internalPos2</head>endPos
        /// </summary>
        public int EndPos
        {
            get { return m_iEndPos; }
            set { m_iEndPos = value; }
        }

        /// <summary>
        /// beginPos<head>internalPos1 internalPos2</head>endPos
        /// </summary>
        public int InternalPos1
        {
            get { return m_iInternalPos1; }
            set { m_iInternalPos1 = value; }
        }

        /// <summary>
        /// beginPos<head>internalPos1 internalPos2</head>endPos
        /// </summary>
        public int InternalPos2
        {
            get { return m_iInternalPos2; }
            set { m_iInternalPos2 = value; }
        }

        public IList<XMLTag> Children
        {
            get { return m_lstChindren; }
            set { m_lstChindren = value; }
        }

        #endregion

        #region method

        /// <summary>
        /// add attribute to a Tag
        /// </summary>
        /// <remarks>
        /// if there is an attriName exists in Tag's Attribute, merge the value
        /// </remarks>
        /// <param name="attriName">attribute name</param>
        /// <param name="attriValue">attribute value</param>
        public void AddAttriute(string attriName, string attriValue)
        {
            //judge if the attriName exists in htTagAttribute
            if (!m_htTagAttribute.Contains(attriName))
            {
                m_htTagAttribute.Add(attriName, attriValue);
            }
            else 
            {
                string strOldVal = Convert.ToString(m_htTagAttribute[attriName]);
                string strNewVal = strOldVal + attriValue;
                m_htTagAttribute[attriName] = strNewVal;
            }
        }

        /// <summary>
        /// get attribute from Tag
        /// </summary>
        /// <param name="attriName">attribute name</param>
        /// <returns>if exists return attribute value then string.Empty</returns>
        public string GetAttribute(string attriName)
        {
            if (m_htTagAttribute.Contains(attriName))
            {
                return Convert.ToString(m_htTagAttribute[attriName]);
            }
            return string.Empty;
        }

        #region override methods

        /// <summary>
        /// override Equals
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if ( obj == null )
                return false;

            if (!(obj is XMLTag))
                return false;

            XMLTag tag = obj as XMLTag;
            //if have the same name,considered to be the same tag!
            if (tag.TagName == this.TagName)
                return true;

            return base.Equals(obj);
        }

        /// <summary>
        /// override GetHashCode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #endregion

        #endregion
    }
}
